//
//  ZBCrossView.m
//  MoBan
//
//  Created by apple on 16/7/6.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZBCrossView.h"
@interface ZBCrossView()
//盛放控件的数组
@property(nonatomic,strong)NSMutableArray *UIarray;
//盛放fram的数组
@property(nonatomic,strong)NSMutableArray *framArray;
@end
@implementation ZBCrossView
-(NSMutableArray *)framArray
{
    if (!_framArray) {
        _framArray = [NSMutableArray array];
    }
    return _framArray;
}
-(NSMutableArray *)UIarray
{
    if (!_UIarray) {
        _UIarray = [NSMutableArray array];
    }
    return _UIarray;

}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
    }
    return self;

}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self ZBaddSubview];

}
/**
 创建控件
 */
-(void)ZBaddSubview
{
    //清空盛放控件的数组
    [self.UIarray makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.UIarray removeAllObjects];
    //获取控制器传入的文字的数组
    NSArray *array =[self.datasouce crossView:self];
    //创建控件
    for (NSInteger i=0; i<array.count; i++) {
        
        UILabel *lable = [[UILabel alloc]init];
        lable.userInteractionEnabled = YES;
        lable.tag = i;
        lable.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lableClic:)];
        [lable addGestureRecognizer:tap];
        
        lable.font = [UIFont systemFontOfSize:13*Kscr];
        lable.text = array[i];
        [self addSubview:lable];
        [self.UIarray addObject:lable];
        
    }
    
    [self ZBcalcuteFram:array];
    
    
    [self ZBSetFram];
}
/**
 计算控件的fram
 */
-(void)ZBcalcuteFram:(NSArray *)array
{

    [self.framArray removeAllObjects];
    
    CGRect afram;
    CGSize asize;
    NSInteger inter = 0;
    for (NSInteger i=0; i<array.count; i++) {
        
        CGSize size =[Unit getSizeWithStrNo:array[i] hight:20*Kscr font:13*Kscr];
        asize = size;
        
        if (i==0) {
            
          CGRect lablefram =  CGRectMake(10*Kscr, 10*Kscr, size.width, 20*Kscr);
            afram = lablefram;
            [self ZBSaveFramArray:lablefram];
            
        }else
        {
            if (asize.width+CGRectGetMaxX(afram)+10*Kscr>KscrW) {
                inter=inter+1;
                
                CGRect lablefram = CGRectMake(10*Kscr, 10*Kscr+25*Kscr*inter, size.width, 20*Kscr);
                afram = lablefram;
                [self ZBSaveFramArray:lablefram];

            }else
            {
            
                CGRect lablefram = CGRectMake(CGRectGetMaxX(afram)+10*Kscr, 10*Kscr+25*Kscr*inter, size.width, 20*Kscr);
                afram = lablefram;
                [self ZBSaveFramArray:lablefram];

            
            }
            
            
        }
        

    }
}
/**
 存放fram的数组
 */
-(void)ZBSaveFramArray:(CGRect)rect
{
    NSValue *value = [NSValue valueWithCGRect:rect];
    [self.framArray addObject:value];
}
/**
 解析数组中的fram
 */
-(CGRect)ZBReadRectFromArray:(NSInteger)index
{
    NSValue *value= self.framArray[index];
    return [value CGRectValue];
}

/**
 设置控件的fram
 */
-(void)ZBSetFram
{

    for (NSInteger i=0; i<self.UIarray.count; i++) {
        
        UILabel *lable = self.UIarray[i];
        lable.frame =[self ZBReadRectFromArray:i];
        
        if (i==self.UIarray.count-1) {
            
        [self.datasouce getHightFromView:self WithViewFloat:lable.bottom+10*Kscr];
        }
        
    }
    
    
}
/**
 
 点击lable的方法
 */
-(void)lableClic:(UIGestureRecognizer *)ges
{
    UILabel *lable = (UILabel *)ges.view;
    if ([self.deleget respondsToSelector:@selector(crossView:didSelectLable:)]) {
        [self.deleget crossView:self didSelectLable:lable.tag];
    }
}
//刷新数据
-(void)reloaddata
{
    [self ZBaddSubview];
}

@end
